﻿package mortal.game.view.group.groupAvatar
{
    import Message.Public.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.mainUI.roleAvatar.*;
    import mortal.mvc.core.*;

    public class GroupAvatarView extends View
    {
        private var _teamMateDic:Dictionary;
        private var _showTrack:Boolean = true;
        private var _bodySprite:GSprite;
        private var _hideBtn:BtnShowTeamAvatar;
        private var _hideTween:TweenMax;
        private var _showTween:TweenMax;

        public function GroupAvatarView()
        {
            this.layer = LayerManager.uiLayer;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bodySprite = UICompomentPool.getUICompoment(GSprite);
            this._bodySprite.createDisposedChildren();
            this._bodySprite.x = 15;
            this._bodySprite.y = 33;
            addChild(this._bodySprite);
            this._hideBtn = UICompomentPool.getUICompoment(BtnShowTeamAvatar);
            this._hideBtn.createDisposedChildren();
            this._hideBtn.x = 0;
            this._hideBtn.y = 140;
            addChild(this._hideBtn);
            this._hideBtn.configEventListener(MouseEvent.CLICK, this.onHideBtnClickHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._bodySprite.dispose(param1);
            this._bodySprite = null;
            this._hideBtn.dispose(param1);
            this._hideBtn = null;
            this.removeAllMate();
            if (this._showTween)
            {
                this._showTween.kill();
                this._showTween = null;
            }
            this._teamMateDic = new Dictionary();
            this._showTrack = true;
            super.disposeImpl();
            return;
        }// end function

        private function getKey(param1:SEntityId) : String
        {
            return param1.id + "_" + param1.type + "_" + param1.typeEx + "_" + param1.typeEx2;
        }// end function

        private function resetPos() : void
        {
            var _loc_4:String = null;
            var _loc_1:* = Cache.instance.group.players;
            var _loc_2:* = _loc_1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                if (EntityUtil.isSelf(_loc_1[_loc_3].player.entityId))
                {
                }
                else
                {
                    _loc_4 = this.getKey(_loc_1[_loc_3].player.entityId);
                    if (this._teamMateDic[_loc_4])
                    {
                        this._teamMateDic[_loc_4].x = 0;
                        this._teamMateDic[_loc_4].y = GameController.avatar.view.y + GameController.avatar.view.height + 45 + (_loc_3 - 1) * 75;
                    }
                }
                _loc_3++;
            }
            return;
        }// end function

        private function removeAllMate() : void
        {
            var _loc_1:GroupAvatar = null;
            for each (_loc_1 in this._teamMateDic)
            {
                
                _loc_1.removeAndStopUpdate();
                _loc_1.dispose(true);
            }
            this._teamMateDic = new Dictionary();
            return;
        }// end function

        private function addTeamMate(param1:SPublicPlayer) : void
        {
            var _loc_2:EntityInfo = null;
            var _loc_3:String = null;
            var _loc_4:GroupAvatar = null;
            if (this._showTrack)
            {
                _loc_2 = Cache.instance.entity.getEntityInfoById(param1.entityId);
                _loc_3 = this.getKey(param1.entityId);
                if (!this._teamMateDic[_loc_3])
                {
                    _loc_4 = UICompomentPool.getUICompoment(GroupAvatar);
                    _loc_4.alpha = 1;
                    if (!Global.isDebugModle)
                    {
                        if (this._bodySprite.numChildren < 4)
                        {
                            this._bodySprite.addChild(_loc_4);
                        }
                    }
                    else
                    {
                        this._bodySprite.addChild(_loc_4);
                    }
                    this._teamMateDic[_loc_3] = _loc_4;
                }
                (this._teamMateDic[_loc_3] as GroupAvatar).updateEntity(_loc_2);
                (this._teamMateDic[_loc_3] as GroupAvatar).updatePlayer(param1);
                if (param1.online == 0)
                {
                    (this._teamMateDic[_loc_3] as GroupAvatar).stopUpdateEntity();
                }
                this.resetPos();
            }
            return;
        }// end function

        public function updateTeamMate() : void
        {
            var _loc_1:Array = null;
            var _loc_2:SGroupPlayer = null;
            if (this._showTrack)
            {
                this.removeAllMate();
                _loc_1 = Cache.instance.group.players;
                for each (_loc_2 in _loc_1)
                {
                    
                    if (EntityUtil.isSelf(_loc_2.player.entityId))
                    {
                        continue;
                    }
                    this.addTeamMate(_loc_2.player);
                }
                if (Cache.instance.group.players.length > 1)
                {
                    this._hideBtn.visible = true;
                }
                else
                {
                    this._hideBtn.visible = false;
                }
            }
            return;
        }// end function

        public function teamMateIn(param1:EntityInfo) : void
        {
            var _loc_2:String = null;
            if (this._showTrack)
            {
                _loc_2 = this.getKey(param1.entityInfo.entityId);
                if (this._teamMateDic[_loc_2])
                {
                    (this._teamMateDic[_loc_2] as GroupAvatar).updateEntity(param1);
                }
            }
            return;
        }// end function

        public function teamMateOut(param1:SEntityId) : void
        {
            var _loc_2:String = null;
            if (this._showTrack)
            {
                _loc_2 = this.getKey(param1);
                if (this._teamMateDic[_loc_2])
                {
                    (this._teamMateDic[_loc_2] as GroupAvatar).stopUpdateEntity();
                }
            }
            return;
        }// end function

        public function removeTeamMate(param1:SEntityId) : void
        {
            var _loc_2:String = null;
            if (this._showTrack)
            {
                _loc_2 = this.getKey(param1);
                if (this._teamMateDic[_loc_2] != null)
                {
                    (this._teamMateDic[_loc_2] as GroupAvatar).removeAndStopUpdate();
                    (this._teamMateDic[_loc_2] as GroupAvatar).dispose(true);
                    delete this._teamMateDic[_loc_2];
                }
                this.resetPos();
            }
            return;
        }// end function

        public function updateTeamMateState(param1:Object) : void
        {
            var playerAvater:GroupAvatar;
            var obj:* = param1;
            var key:* = this.getKey(obj.id);
            playerAvater = this._teamMateDic[key];
            if (playerAvater)
            {
                var removeEffect:* = function () : void
            {
                EffectManager.glowFilterUnReg(playerAvater);
                return;
            }// end function
            ;
                switch(obj.type)
                {
                    case EEntityAttribute._EAttributeGroupMemberOnline:
                    {
                        playerAvater.publicPlayer.online = obj.value;
                        playerAvater.updateOnline();
                        break;
                    }
                    case EEntityAttribute._EAttributeSpaceId:
                    {
                        playerAvater.updateMapId(obj.value);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function onHideBtnClickHandler(event:MouseEvent) : void
        {
            if (this._showTrack)
            {
                this.hideTrack();
            }
            else
            {
                this.showTrack();
            }
            return;
        }// end function

        public function set showList(param1:Boolean) : void
        {
            if (this.isHide)
            {
                return;
            }
            if (this._showTrack == param1)
            {
                return;
            }
            if (param1)
            {
                this.showTrack();
            }
            else
            {
                this.hideTrack();
            }
            return;
        }// end function

        public function showTrack() : void
        {
            var _loc_4:String = null;
            if (this._hideTween && this._hideTween.active)
            {
                this._hideTween.kill();
            }
            if (!this._bodySprite.parent)
            {
                addChild(this._bodySprite);
            }
            x = 0;
            y = 0;
            this._hideBtn.show = true;
            this._showTrack = true;
            this.updateTeamMate();
            var _loc_1:* = Cache.instance.group.players;
            var _loc_2:* = _loc_1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                if (EntityUtil.isSelf(_loc_1[_loc_3].player.entityId))
                {
                }
                else
                {
                    _loc_4 = this.getKey(_loc_1[_loc_3].player.entityId);
                    if (this._teamMateDic[_loc_4])
                    {
                        this._showTween = TweenMax.to(this._teamMateDic[_loc_4], _loc_3 * 0.1, {x:0, y:this._teamMateDic[_loc_4].y, alpha:1, ease:Quint.easeIn, onComplete:this.onShowEnd, onCompleteParams:[_loc_3]});
                    }
                }
                _loc_3++;
            }
            return;
        }// end function

        public function hideTrack() : void
        {
            var _loc_4:String = null;
            if (this._showTween && this._showTween.active)
            {
                this._showTween.kill();
            }
            x = 0;
            y = 0;
            this._hideBtn.show = false;
            this._showTrack = false;
            var _loc_1:* = Cache.instance.group.players;
            var _loc_2:* = _loc_1.length;
            var _loc_3:int = 1;
            while (_loc_3 < _loc_2)
            {
                
                if (EntityUtil.isSelf(_loc_1[_loc_3].player.entityId))
                {
                }
                else
                {
                    _loc_4 = this.getKey(_loc_1[_loc_3].player.entityId);
                    if (this._teamMateDic[_loc_4])
                    {
                        this._hideTween = TweenMax.to(this._teamMateDic[_loc_4], _loc_3 * 0.1, {x:-this._teamMateDic[_loc_4].width, y:this._teamMateDic[_loc_4].y, alpha:0, ease:Quint.easeIn, onComplete:this.onHideEnd, onCompleteParams:[_loc_3]});
                    }
                }
                _loc_3++;
            }
            return;
        }// end function

        private function onShowEnd(param1:int) : void
        {
            return;
        }// end function

        private function onHideEnd(param1:int) : void
        {
            this.removeAllMate();
            if (param1 == (Cache.instance.group.players.length - 1))
            {
                removeChild(this._bodySprite);
            }
            return;
        }// end function

        public function addChatTxtByID(param1:SEntityId, param2:String) : void
        {
            var _loc_3:String = null;
            var _loc_4:GroupAvatar = null;
            if (this._showTrack)
            {
                _loc_3 = this.getKey(param1);
                _loc_4 = this._teamMateDic[_loc_3];
                if (_loc_4)
                {
                    _loc_4.addChatData(param2);
                }
            }
            return;
        }// end function

    }
}
